"""
@name : 11-12 ShoppingSystem
@author : hj
@projectname: CJH
"""
import sys


def slshoppingsystem():
    login()
    budget()
    choice = input("请选择：")
    if choice == '1':
        goodsinfo()
        purchasedinfo()
    elif choice == '2':
        purchasedinfo()
    elif choice == 'exit':
        slexit()
    else:
        print("请选择1 或 2")


def login():
    while True:
        print("**即将进入三乐购物系统**")
        user_name = input("请输入你的用户名：")
        user_passwd = input("请输入你的密码：")
        if user_name == "exit" or user_passwd == "exit":
            print("退出")
            sys.exit()
        elif user_name == 'root' and user_passwd == '123456':
            print("系统登入成功！")
            break
        else:
            print("密码或者用户名输入有误，请重新输入！")
    print("三乐购物系统".center(48, "*"))


def budget():
    global money
    while True:
        money = input("请输入您的预算：")
        if money.isdigit():
            break
        elif money == 'exit':
            slexit()
        else:
            print("输入错误！请重新输入！")

    print("1、查看商品信息")
    print("2、查看已购信息")


shoopping_dict = {}


def goodsinfo():
    fruits = {'F00001': {'name': '苹果', 'price': '3.5'}, 'F00002': {'name': '香蕉', 'price': '2'},
              'F00003': {'name': '梨子', 'price': '5'}, 'F00004': {'name': '西瓜', 'price': '2.5'}}
    for i in fruits.keys():
        print(f"{i}: {fruits.get(i).get('name')}", "  ", f"价格:{fruits.get(i).get('price')}元/斤")
    print("您可输入商品编号进行购买商品，或输入exit 退出当前页面")

    goodsid = input("请输入：")
    if goodsid == 'exit':
        slexit()
    elif goodsid in fruits.keys():
        num = int(input(f"请选择要购买{fruits.get(goodsid).get('name')}的数量:"))
        global yue
        if num * float(fruits.get(goodsid).get('price')) > float(money):
            print("你的预算不足，请重新选择")
        else:
            yue = float(money) - num * float(fruits.get(goodsid).get('price'))
            print(f"您的资金充足，已扣款{num * float(fruits.get(goodsid).get('price'))}元，剩余{yue}元")
            shoopping_dict[fruits.get(goodsid).get('name')] = num * float(fruits.get(goodsid).get('price'))
    choice2 = input("请按exit退出  或 按0 返回上一级菜单")
    if choice2 == 'exit':
        slexit()
    elif choice2 == '0':
        pass
    else:
        pass


def purchasedinfo():
    for i in shoopping_dict.keys():
        print(f"已购商品信息：\n {i}：{shoopping_dict.get(i)}元")


def slexit():
    print("退出")
    exit(0)
    purchasedinfo()
    print(f"{yue}")


slshoppingsystem()

